/*
 * ObjectDatabase.c
 * Behavioral Attributed Automaton
 * @author Gregoire JACOB (gregoire.jacob@orange-ftgroup.com)
 * @date 02/12/2008
 * @version 2.1
 * Object database related to the parsed trace
 */

#include ".\Includes\BehaviorAutomata.h"


void loadObjectDatabase(FILE * odb){
	int dbsize = 0, i, j; 
	char rchar; int size;
	char name[NAME_MAX_LENGTH];
	
	//Allocating database space
	fread(&dbsize,sizeof(int),1,odb);
	//printf("[+] database size? %d\n",dbsize);
	ps_ObjDB = (struct OBJ_ENTRY *)calloc(dbsize,sizeof(struct OBJ_ENTRY));
	if(ps_ObjDB==NULL){
		printf("[-] Error when allocating the object database\n");
		exit(0);
	}//if

	//Recovering object entries
	for(i=0; i<dbsize; i++){
		//Parsing name if required
		rchar = 0; 
		fread(&rchar,sizeof(char),1,odb);
		if(rchar=='n'){
			ps_ObjDB[i].ObjectName = (char *) calloc(NAME_MAX_LENGTH,sizeof(char));
			fgets(name,NAME_MAX_LENGTH,odb);
			size = (int)strlen(name);
			for(j=size-1;j>size-4;j--) //Removing blanks 
				if(name[j]==' '||name[j]=='\n'){name[j]=0;}else{break;}
			for(j=0;j<size;j++)
				if(name[j]!=' ') break;
			strcpy_s(ps_ObjDB[i].ObjectName,NAME_MAX_LENGTH,&name[j]);
		}
		fread(&ps_ObjDB[i].Type,sizeof(int),1,odb);
		fread(&ps_ObjDB[i].Nature,sizeof(int),1,odb);
		fread(&ps_ObjDB[i].Status,sizeof(int),1,odb);
		//printf("[+] Recovered entry n%d: ",i);
		//if(rchar == 'n') printf("%s ",ps_ObjDB[i].ObjectName);
		//printf("%d %d %d\n",ps_ObjDB[i].Type,ps_ObjDB[i].Nature,ps_ObjDB[i].Status);
	}//for
}


